package lol

import (
	"gorm.io/gorm/clause"
	"longmen/server/config/global"
	"longmen/server/internal/services/nami/utils"
	"longmen/server/pkg/db/mysql/models/models_esports"
)

type InsertTeamInterface interface {
	Get() models_esports.LolTeam
}

func CrateTeam(a []InsertTeamInterface) {
	records := make([]models_esports.LolTeam, 0)
	for _, c := range a {
		records = append(records, c.Get())
	}
	if len(records) == 0 {
		return
	}
	clauses := clause.OnConflict{
		UpdateAll: true,
	}
	err := global.Sport().Model(records).Clauses(clauses).CreateInBatches(records, 100).Error
	if err != nil {
		return
	}
}
func GetTeamTime() int {
	var data models_esports.LolTeam
	err := global.Sport().Model(&data).Order("updated_at DESC").Limit(1).Take(&data).Error
	if err != nil {
		return 0
	}
	return data.UpdatedAt
}

func GetTeamIdMaps(ids []int) (map[int]models_esports.LolTeam, error) {
	var records []models_esports.LolTeam
	data := make(map[int]models_esports.LolTeam)
	queryIds := utils.RemoveDuplicatesAndEmpty(ids)
	// 空数据不查询
	if len(queryIds) == 0 {
		return data, nil
	}
	err := global.Sport().Model(&records).Where("id in ?", queryIds).Find(&records).Error
	if err != nil {
		return nil, err
	}

	for i, record := range records {
		data[record.ID] = records[i]
	}
	return data, nil
}
